\documentclass[10pt,a4paper]{article} 

\usepackage{ctex} % 中文支持
\usepackage[top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry} % 页边距
\usepackage{amsmath, amssymb} % 数学公式与符号
\usepackage{graphicx}

\usepackage{pythonhighlight}
\usepackage{url} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 使表格美观
\usepackage{array}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
%\newcolumntype{N}{@{}m{0pt}@{}}
\setlength\extrarowheight{3pt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{titling}
\setlength{\droptitle}{-2cm} % 标题上移 

%%文档的题目、作者与日期
\author{五六七 }
\title{多点测距定位 }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\maketitle

\begin{abstract}
已知不同的观测站与未知位置的距离，求该未知的位置。
\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\setcounter{tocdepth}{2}
%\renewcommand\contentsname{目录}
%
%\renewcommand {\baselinestretch} {1.3}\normalsize 
%\tableofcontents 
%\renewcommand {\baselinestretch} {1.0}\normalsize


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{问题描述}
设在平面上考虑问题。已知四个观测站的位置坐标 $(x_i,y_i), \,\, i=1,2,3,4$. 
已知每个观测站到某个未知信号的距离 $d_i,\,\, i=1,2,3,4$. 求未知信号的位置坐标。
\begin{table}[ht]
\centering
%\caption{观测站的位置与测距}
\begin{tabular}{|M{2cm}|M{1.5cm}|M{1.5cm}|M{1.5cm}|M{1.5cm}|} \hline 
观测站编号&1&2&3&4 \\ \hline 
$x_i$ & 245 & 164 & 192 & 232  \\ \hline 
$y_i$ & 442 & 480 & 281 & 300 \\ \hline 
$d_i$ & 126.2204 & 120.7509 & 90.1854 & 101.4021  \\ \hline 
\end{tabular}
\end{table}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{建立模型}
设未知信号所在位置的坐标为 $(x,y)$. 则根据欧氏空间的距离公式，可得
\begin{eqnarray}
\sqrt{(x-x_1)^2+(y-y_1)^2} &=& d_1, \\ 
\sqrt{(x-x_2)^2+(y-y_2)^2} &=& d_2, \\ 
\sqrt{(x-x_3)^2+(y-y_3)^2} &=& d_3, \\ 
\sqrt{(x-x_4)^2+(y-y_4)^2} &=& d_4. 
\end{eqnarray}
代入数据，可得关于未知数 $(x,y)$ 的四个方程。所以这是超定的方程组，一般求不出严格的解。
为了求出近似的数值解，我们考虑最小二乘法，把问题化为求下述函数的最小值，
\begin{eqnarray}
f(x,y) &=&  
\left[ \sqrt{(x-x_1)^2+(y-y_1)^2} - d_1\right]^2 
+ \left[ \sqrt{(x-x_2)^2+(y-y_2)^2} - d_2\right]^2  \nonumber \\ 
&& + \left[ \sqrt{(x-x_3)^2+(y-y_3)^2} - d_3\right]^2 
+ \left[ \sqrt{(x-x_4)^2+(y-y_4)^2} - d_4\right]^2.  
\end{eqnarray}
%或者考虑下述函数的最小值，
%\begin{eqnarray}
%g(x,y) &=&  
%\left[ (x-x_1)^2+(y-y_1)^2 - d_1^2\right]^2
%+ \left[ (x-x_2)^2+(y-y_2)^2 - d_2^2 \right]^2  \nonumber \\ 
%&& + \left[ (x-x_3)^2+(y-y_3)^2 - d_3^2\right]^2 
%+ \left[ (x-x_4)^2+(y-y_4)^2 - d_4^2\right]^2.  
%\end{eqnarray}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{编程计算}
首先载入需要的程序包，以及最小二乘法优化函数 \texttt{least\_square}. 
\begin{python}
import numpy as np
from scipy.optimize import least_squares
\end{python}

输入已知的四个观测站的坐标数据，以及四个距离数据。
\begin{python}
x0=np.array([245,164,192,232])
y0=np.array([442,480,281,300])
d=np.array([126.2204,120.7509,90.1854,101.4021])
\end{python}

定义目标函数 fx, 注意函数的自变量为 x, 它有两个分量，分别是 x[0] 和 x[1]. 
注意到这个函数里并没有出现求和，但是变量 x0, y0 和 d 是前面定义的各自有四个分量的已知数值。
\begin{python}
fx=lambda x:np.sqrt((x0-x[0])**2+(y0-x[1])**2)-d
\end{python}

调用最小二乘法优化函数，自变量 x 的的两个分量的初始值都设为 0.5. 
\begin{python}
s=least_squares(fx, np.array([0.5,0.5]))
\end{python}

打印输出结果。
\begin{python}
print(s)
print(s.x)
\end{python}

查看变量 s 的数据类型，这是optimize 程序包自定义的数据类型。
\begin{python}
In[11]: type(s)
Out[11]: scipy.optimize.optimize.OptimizeResult
\end{python}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{回答问题}
未知信号所在的坐标为 
\begin{eqnarray}
x &=& 149.5089, \\
y &=& 359.9848.
\end{eqnarray}

画出四个观测站和未知信号的位置如下。
\begin{center}
\includegraphics [height=5cm, width=8cm]{five_points_four_lines.png}
\end{center}

画图使用代码如下。
\begin{python}
import numpy as np
import matplotlib.pyplot as plt

x0=np.array([245,164,192,232])
y0=np.array([442,480,281,300])

x=149.5089 
y=359.9848

plt.plot(x0,y0,'bo')
plt.plot(x,y,'ro')
for k in range(4):
    plt.plot([x0[k],x],[y0[k],y],'b-')
\end{python}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\section{参考文献 }
\begin{thebibliography}{99}
\bibitem{sishoukui-2} 司守奎,孙玺菁. \emph{Python数学建模算法与应用}, 国防工业出版社. 2022年1月第1版. 

\end{thebibliography}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


